/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.group;

import com.workcell.workdata.im.model.request.GroupNoteRequest;
import com.workcell.workdata.im.model.response.PaginationEntity;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.NoticeVO;
import com.workcell.workdata.im.service.GroupNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/group")
@Api(produces = "application/json", tags = "群公告相关接口" )
@Validated
public class NoticeController {

	@Resource
	private GroupNoticeService groupNoticeService;


	@ApiOperation(httpMethod = "POST", value = "发布公告")
	@PostMapping(value = "/notice/add")
	public ResponseEntity<Void> add(@RequestBody GroupNoteRequest request) {

		groupNoticeService.update(request.getGroupId(), request.getText());

		return ResponseEntity.ok();
	}


	@ApiOperation(httpMethod = "PATCH", value = "修改群组公告")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0"),
			@ApiImplicitParam(name = "notice", value = "公告", paramType = "query", dataTypeClass = String.class),
	})
	@PatchMapping(value = "/{id}/notice")
	@Deprecated
	public ResponseEntity<Void> updateNotice(@PathVariable Long id, @RequestParam String  notice) {

		groupNoticeService.update(id, notice);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "GET", value = "查询群公告历史")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "groupId", value = "群组ID", paramType = "query",  dataTypeClass = Long.class,example = "0"),
			@ApiImplicitParam(name = "currentPage", value = "当前页数，起始0", paramType = "query", dataTypeClass = String.class),
	})
	@GetMapping(value = "/notice/list")
	public PaginationEntity<NoticeVO> page(@RequestParam Long groupId, @RequestParam int currentPage) {
		Page<NoticeVO> page =  groupNoticeService.queryPage(groupId, currentPage);
		return PaginationEntity.make(page);
	}
}
